﻿  using System;
using System.Collections.Generic;
using System.Configuration.Install;
using System.ServiceProcess;
using System.Text;

namespace QT.Windows
{
    /// <summary>
    /// 服务操作类
    /// </summary>
    public class Services
    {
        /// <summary>
        /// 判断服务是否存在
        /// </summary>
        /// <param name="ServiceName">服务名称</param>
        /// <returns>bool</returns>
        public static bool Exists(string ServiceName)
        {
            ServiceController[] services = ServiceController.GetServices();
            foreach (ServiceController sc in services)
            {
                if (sc.ServiceName == ServiceName)
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 安装服务
        /// </summary>
        /// <param name="stateSaver"></param>
        /// <param name="servicename">服务名称</param>
        /// <param name="filename">文件路径</param>
        /// <param name="param">启动参数</param>
        /// <returns>bool</returns>
        public static bool Install(System.Collections.IDictionary stateSaver, string servicename, string filename, string[] param)
        {
            try
            {
                ServiceController sc = new ServiceController(servicename);
                if (!Exists(servicename))
                {
                    AssemblyInstaller ai = new AssemblyInstaller();
                    ai.UseNewContext = true;
                    ai.Path = filename;
                    ai.CommandLine = param;
                    ai.Install(stateSaver);
                    ai.Commit(stateSaver);
                    ai.Dispose();
                    sc.Start();
                }
                else
                {
                    if (sc.Status != ServiceControllerStatus.Running && sc.Status != ServiceControllerStatus.StartPending)
                    {
                        sc.Start();
                    }
                }
            }
            catch
            {
                return false;
            }

            return true;
        }

        /// <summary>
        /// 卸载服务
        /// </summary>
        /// <param name="servicename">服务名称</param>
        /// <returns>bool</returns>
        public static bool UnInstall(string servicename)
        {
            try
            {
                if (Exists(servicename))
                {
                    AssemblyInstaller ai = new AssemblyInstaller();
                    ai.UseNewContext = true;
                    //ai.Path = filepath;
                    ai.Uninstall(null);
                    ai.Dispose();
                }
            }
            catch
            {
                return false;
            }

            return true;
        }

        /// <summary>
        /// 启动服务
        /// </summary>
        /// <param name="servicename">服务名称</param>
        /// <returns>bool</returns>
        public static bool Start(string servicename)
        {
            if (Exists(servicename))
            {
                ServiceController sc = new ServiceController(servicename);
                if (sc.Status != ServiceControllerStatus.Running && sc.Status != ServiceControllerStatus.StartPending)
                {
                    sc.Start();
                    for (int i = 0; i < 60; i++)
                    {
                        sc.Refresh();
                        System.Threading.Thread.Sleep(1000);
                        if (sc.Status == ServiceControllerStatus.Running)
                        {
                            return true;
                        }
                        if (i == 59)
                        {
                            return false;
                        }
                    }
                }

                return false;
            }

            return false;
        }

        /// <summary>
        /// 停止服务
        /// </summary>
        /// <param name="servicename">服务名称</param>
        /// <returns>bool</returns>
        public static bool Stop(string servicename)
        {
            if (Exists(servicename))
            {
                ServiceController sc = new ServiceController(servicename);
                if (sc.Status == ServiceControllerStatus.Running)
                {
                    sc.Stop();
                    for (int i = 0; i < 60; i++)
                    {
                        sc.Refresh();
                        System.Threading.Thread.Sleep(1000);
                        if (sc.Status == ServiceControllerStatus.Stopped)
                        {
                            return true;
                        }
                        if (i == 59)
                        {
                            return false;
                        }
                    }
                }

                return false;
            }

            return false;
        }
    }
}
